package 剑指Offer.medium;

public class 树的子结构 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    class Solution {
        public boolean isSubStructure(TreeNode A, TreeNode B) {
            if (A == null || B == null) {
                return false;
            }

            return recurCharge(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
        }

        public boolean recurCharge(TreeNode a, TreeNode b) {
            if (b == null) {
                return true;
            }
            if (a == null || a.val != b.val) {
                return false;
            }

            return recurCharge(a.left, b.left) && recurCharge(a.right, b.right);
        }
    }
}
